package com.powerunion.datacollection.web.struts;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.powerunion.datacollection.service.UsersRolesManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.log.LogHelper;

public class UsersRolesAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(UsersRolesAction.class);
	private UsersRolesManager usersRolesManager = null;
	
	public UsersRolesAction() {
		super();
		this.usersRolesManager = new UsersRolesManager();
	}
	
	public void finalize() throws Throwable {
		this.usersRolesManager = null;
		super.finalize();
	}
	
//	public ActionForward save (ActionMapping mapping,ActionForm form,
//			HttpServletRequest request,HttpServletResponse response)
//	        throws Exception {
//		
//		String userStrs = request.getParameter("userStrs");
//		
//	    String roleId = request.getParameter("roleId");
//	    
//		Transaction transaction = null;
//		try {
//			transaction = TransactionHelper.getTransaction();
//			transaction.begin();
//			
//			boolean isSuccesss = this.usersRolesManager.save(userStrs, roleId);
//			
//	        if(isSuccesss) {
//	        	response.getWriter().write("{success:true}");
//	        }else{
//	        	response.getWriter().write("{failure:true}");
//	        }
//			
//			transaction.commit();
//		} catch (Exception e) {
//			try {
//				transaction.rollback();
//			} catch (Exception e1) {
//				logger.error("", e);
//				throw e;
//			} finally {
//				transaction = null;
//				response.getWriter().write("{failure:true}");
//			}
//		}
//
//		return null;
//	}
	
	/**
	 * 根据角色ID查找对应的用户ID
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
//	public ActionForward getUsersByRole (ActionMapping mapping,ActionForm form,
//			HttpServletRequest request,HttpServletResponse response)
//	        throws Exception {
//		
//		response.setContentType("text/json;charset=utf-8");
//		//得到前台传来的id值查询相应的数据，并返回json格式的数据给前台
//		String roleId = request.getParameter("roleId");
//		StringBuffer data = new StringBuffer();
//
//		JSONArray jsonArray = this.usersRolesManager.getUsersByRole(roleId);
//		if(jsonArray != null) {
//			data.append("{success:true,results:").append(jsonArray).append(" }");
//		}else {
//			data.append("{failure:true}");
//		}
//		
//
//		String str = data.toString();
//		System.out.println("角色ID为:" + roleId + "的用户为" + str);
//		try {
//			response.getWriter().print(str);
//		} catch (IOException e) {
//			logger.error("输出角色ID为："+roleId+"的用户失败" + e.getMessage());
//			return null;
//		}
//		
//		return null;
//	}
	
	
	
}
